package bsc.war.form;

import bsc.ejb.entity.DimensionalCut;
import bsc.ejb.entity.Map;
import bsc.ejb.entity.MapDrawingSettings;
import bsc.ejb.entity.ScoreCard;
import bsc.ejb.metadata.MapDrawing;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.ScoreCardUtils;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author castrosalazr
 */
public class MapDesigner extends AbstractController {

    public MapDesigner() {
    }

    @Override
    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        java.util.Map<String, Object> params = new HashMap<String, Object>();
        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            Map currentMap = delegate.findMap(Long.parseLong(request.getParameter("mapId")));
            ;
            params.put("mapId", currentMap.getIdScoreCard().toString());
            request.setAttribute("map", currentMap);

            MapDrawing drawing = currentMap.getMapDrawing();
            request.setAttribute("drawing", drawing);

            List<ScoreCard> semaphores = ScoreCardUtils.getScoreCards(delegate, currentMap);
            java.util.Map<ScoreCard, Double> left = new HashMap<ScoreCard, Double>();
            java.util.Map<ScoreCard, Double> top = new HashMap<ScoreCard, Double>();

            for (ScoreCard semaphore : semaphores) {
                MapDrawingSettings mapDrawingSettings = (MapDrawingSettings) delegate.findMapDrawingSettings(semaphore.getSettings(), drawing);
                if (mapDrawingSettings == null) {
                    mapDrawingSettings = (MapDrawingSettings) Factory.getBean("MapDrawingSettings");
                    mapDrawingSettings.setMapDrawing(drawing);
                    mapDrawingSettings.setSettings(semaphore.getSettings());
                    mapDrawingSettings.setX(-1000d);
                    mapDrawingSettings.setY(-1000d);
                    delegate.createMapDrawingSettings(mapDrawingSettings);
                }
                left.put(semaphore, mapDrawingSettings.getX());
                top.put(semaphore, mapDrawingSettings.getY());
            }

            if (request.getParameterValues("semaphores") != null && request.getParameterValues("semaphores").length > 0) {
                for (String semaphore : request.getParameterValues("semaphores")) {
                    ScoreCard scoreCard = delegate.findScoreCard(Long.parseLong(semaphore));
                    MapDrawingSettings mapDrawingSettings = delegate.findMapDrawingSettings(scoreCard.getSettings(), drawing);
                    Double x = Double.parseDouble(request.getParameter("left-" + semaphore));
                    Double y = Double.parseDouble(request.getParameter("top-" + semaphore));
                    mapDrawingSettings.setX(x);
                    mapDrawingSettings.setY(y);
                    delegate.updateMapDrawingSettings(mapDrawingSettings);
                }
            }

            //List of all DimensionalCuts
            List<DimensionalCut> dimensionalCuts = ScoreCardUtils.getDimensionalCuts(request, delegate);
            //Se obtiene los strings de los cuts
            java.util.Map<DimensionalCut, String> dimensionalCutsMembers = ScoreCardUtils.getDimensionalCutsMembersStrings(request, delegate, dimensionalCuts);
            request.setAttribute("dimensionalCutsMembers", dimensionalCutsMembers);

            request.setAttribute("semaphores", semaphores);
            request.setAttribute("left", left);
            request.setAttribute("top", top);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Caught exception", e);
        }

        if (request.getParameterValues("semaphores") != null &&
                request.getParameterValues("semaphores").length > 0) {
            return new ModelAndView(new RedirectView("mapDesigner.do"), params);
        } else {
            return new ModelAndView("mapDesigner");
        }
    }
}
